package com.anbinh.doctor.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.FragmentMain;
import com.anbinh.doctor.fragment.lichhen.FragmentLichhen;
import com.anbinh.doctor.model.Address.Data.Item;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class AddressExamAdapter extends ArrayAdapter<Item> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Item> listData;
	private int idCheked;
	private Context mContext;
	private UserLogin userLogin;
	private IRcallBak callBack;

	public AddressExamAdapter(Context mContext, int resource,
			List<Item> models, int idChecked, IRcallBak caBak) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		this.idCheked = idChecked;
		this.callBack = caBak;
		layoutInflater = LayoutInflater.from(mContext);
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
	}

	public class ViewHolder {
		public TextView lblLocationName;
		public TextView lblLocationAddress;
		public TextView lblLocationPhone;
		public TextView isActive;
		private View btnSetup;
		private View view_setup;
		
		private View drag_handle;
		public ToggleButton onoff;

	}

	@Override
	public Item getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.lblLocationName = (TextView) convertView
					.findViewById(R.id.lblLocationName);

			viewHolder.lblLocationAddress = (TextView) convertView
					.findViewById(R.id.lblLocationAddress);

			viewHolder.lblLocationPhone = (TextView) convertView
					.findViewById(R.id.lblLocationPhone);

			viewHolder.isActive = (TextView) convertView
					.findViewById(R.id.isActive);
			viewHolder.btnSetup = (View) convertView
					.findViewById(R.id.btnSetup);
			viewHolder.drag_handle = (View) convertView
					.findViewById(R.id.drag_handle);
			viewHolder.view_setup = (View) convertView
					.findViewById(R.id.view_setup);

			
			viewHolder.onoff = (ToggleButton) convertView
					.findViewById(R.id.onoff);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Item item = getItem(position);

		viewHolder.lblLocationName.setText(item.getClinic_name()+ item.getId());
		viewHolder.lblLocationAddress.setText(item.getAddress());

		if (item.getIs_default() == 1) {
			viewHolder.isActive.setVisibility(View.VISIBLE);
		} else {
			viewHolder.isActive.setVisibility(View.GONE);
		
		}
		
		if (item.isEdit()) {
			viewHolder.view_setup.setVisibility(View.INVISIBLE);
			viewHolder.drag_handle.setVisibility(View.VISIBLE);
		} else {
			viewHolder.drag_handle.setVisibility(View.GONE);
			viewHolder.view_setup.setVisibility(View.VISIBLE);
		}

		if (item.getPhone().isEmpty()) {
			viewHolder.lblLocationPhone.setText(mContext.getResources()
					.getString(R.string.so_dien_thoai_ccn));
		} else {
			viewHolder.lblLocationPhone.setText(item.getPhone());
		}

		if (item.getIs_active() == 1) {
			viewHolder.onoff.setToggleOn();
		} else {
			viewHolder.onoff.setToggleOff();
		}
		viewHolder.onoff.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				int is_Active = 0;
				if (on) {
					is_Active = 1;

				} else {
					is_Active = 0;
				}
				onOffLocation(is_Active, item);

			}
		});
		/*
		 * if (item.isEdit()) { viewHolder.btnSetup.setVisibility(View.VISIBLE);
		 * } else { viewHolder.btnSetup.setVisibility(View.GONE); }
		 */

		viewHolder.btnSetup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callBack.onDisplay(position, null);
			}
		});

		return convertView;
	}

	public void onOffLocation(int is_Active, Item item) {

		ServerController.getInstance(mContext).reuquestAddressOnOff(
				userLogin.getData().getToken(), item.getId(), is_Active,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						FragmentMain.newInstance().setRefreshAppointMent(true);
					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}
}
